package com.jrx.anytxn.transaction.timer.job.autoPaymentFile;


import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.service.IVerifyFileService;
import com.jrx.anytxn.transaction.timer.config.VerifyFileConfig;
import com.jrx.anytxn.transaction.timer.job.allocation.config.AllocationConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 批扣对账文件生成
 */
@Configuration
@EnableConfigurationProperties(VerifyFileConfig.class)
public class AutoPaymentFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(AllocationConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IProductTableService productTableService;
    @Autowired
    private IVerifyFileService verifyFileService;
    @Autowired
    private VerifyFileConfig verifyFileConfig;


    @Bean
    public Job autoPaymentFileJob() {
        return jobBuilderFactory.get("autoPaymentFileJob")
                .start(autoPaymentFileStep())
                .build();
    }

    @Bean
    public Step autoPaymentFileStep() {
        return stepBuilderFactory.get("autoPaymentFileStep")
                .tasklet(tasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet tasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("开始生成批扣对账文件");
            List<PrProductTable> productTableList = productTableService.findAllVerifyForBatch(TransBizConstant.STATUS_1,TransBizConstant.STATUS_1);
            verifyFileService.createAutoPaymentFile(productTableList,verifyFileConfig.getFilePath(),verifyFileConfig.getFileName(),verifyFileConfig.getVerifyPoolSize());
            return RepeatStatus.FINISHED;
        };
    }
}
